package com.xlh.server.protocol.support.json;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Html;
import android.text.SpannableString;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.xlh.Utils.Constant;
import com.xlh.Utils.FUti;
import com.xlh.Utils.FontHandler;
import com.xlh.Utils.GsonManager;

import com.xlh.bean.ProtocolObject.ButsGrop;

import com.xlh.bean.ProtocolObject.InputBean;
import com.xlh.bean.ProtocolObject.ParamObject.ButsGropObject;
import com.xlh.bean.ProtocolObject.ParamObject.InputParam;
import com.xlh.bean.TitleBean;
import com.xlh.interf.IActivity;
import com.xlh.interf.ITakeMessage;

import com.xlh.outside.R;
import com.xlh.view.FFButton;

import net.nightwhistler.htmlspanner.HtmlSpanner;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2018/7/2.
 */

public class ButsGropServer implements ITakeMessage {

    private IActivity atv;
    private String hasType = "butsGrop";
    private ButsGrop protccoObj;
    public ButsGropServer(IActivity atv){
        this.atv = atv;


    }

    @Override
    public void takeMessage(String t,String s){

        if(!t.equals(this.hasType)) return;

        protccoObj =  GsonManager.gsmgr.jsonToAnyObject(s, ButsGrop.class);

        if(protccoObj==null || protccoObj.getButs().getFooterButs()==null) return;

        setUserButs(protccoObj.getButs().getUserButs());
        setFooterButs(protccoObj.getButs().getFooterButs());


    }

    @Override
    public boolean takeMessage(String s) {
        return false;
    }

    @Override
    public void init() {

    }

    @Override
    public void atViewLoadAfterInit() {

    }

    //设定底部按钮
    public void setFooterButs(List<ButsGropObject> footerButs){
        final ButsGropServer me = this;
        LinearLayout.LayoutParams params=null;

        if(footerButs==null || footerButs.size()<=0) return;
        String tStr="",aryStr[];
        for (int i = 0; i < footerButs.size(); i++) {

            final FFButton tvBtn = new FFButton(this.atv.getActivity());
            //按钮中换行标记
            tStr = footerButs.get(i).getLabel();
            if(tStr.indexOf("\n") > -1){
                aryStr = footerButs.get(i).getLabel().split("\n");
                tStr = aryStr[0];
            }


            tvBtn.setMinHeight(0);
            tvBtn.setMinWidth(0);
            tvBtn.setText(Html.fromHtml("<font color='cyan'>" + tStr + "</font>"));
            tvBtn.setTag(footerButs.get(i).getCmd());
            //服务端的按钮编号都是从1开始的
            //底部按钮是从21开始编号
            tvBtn.setId(30+(i+1));


            //SDK小于18的 也就是4.3 4.2 和以下的 显示有问题
            //if (Integer.parseInt(FUti.getSDKVersionNumber())<=18){
            //tvBtn.setPadding(0, 0, 0, 0);
            //tvBtn.setTextSize(10);
            //}else{
            params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            tvBtn.setBackgroundResource(R.drawable.sys_menu_btn);
            tvBtn.setTextSize(12);
            tvBtn.setPadding(0, 0, 0, 0);

            params.width=  (this.atv.getActivity().getApp().getWndWidth()-5) / 7;
            tvBtn.setLayoutParams(params);

            GradientDrawable drawable = (GradientDrawable) tvBtn.getBackground();
            //设置边框
            drawable.setStroke(FUti.dip2px(this.atv.getActivity(),1), Color.parseColor(Constant.getAppColor().getButBorderColor()));
            //设置背景
            drawable.setColor(Color.parseColor(Constant.getAppColor().getButBackColor()));


            this.atv.getActivity().mBtnGroup.addView(tvBtn);
            tvBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    me.atv.getActivity().getmAlertGrid().getDataAdapter().clear();
                    me.atv.getActivity().getmAlertList().getDataAdapter().clear();
                    me.atv.getActivity().closeAlertBox();

                    if (me.atv.getActivity().mSocketClient != null) {
                        // TODO Auto-generated method

                        me.atv.getActivity().currentFooterButAction = tvBtn.getTag().toString();

                        //激活全局按钮属性
                        me.atv.getActivity().setActiveButKey(String.valueOf(tvBtn.getId()));
                        if(tvBtn.getId()==Integer.valueOf("31")){
                            InputBean inputBean = new InputBean();
                             InputParam inputParam = new InputParam();
                             inputParam.setCmd("");
                             inputParam.setHitText("请输入指令");
                             inputParam.setDesc("");
                             inputBean.setInput(inputParam);
                            ((InputBoxServer)me.atv.getActivity().getTakeMessageHandler().get("InputBoxServer")).setProtccoObj(inputBean).renderView();

                        }else{
                            me.atv.getActivity().mSocketClient.sendMsg(tvBtn.getTag());
                        }



                    }
                }
            });

        }//for结束

        Button buts = new Button(this.atv.getActivity());
        buts.setText(Html.fromHtml("<font color='cyan'>配色</font>"));


        buts.setBackgroundResource(R.drawable.btn);
        buts.setTextSize(12);
        buts.setPadding(0, 0, 0, 0);

        buts.setLayoutParams(params);

        GradientDrawable drawable = (GradientDrawable) buts.getBackground();
        //设置边框
        drawable.setStroke(FUti.dip2px(this.atv.getActivity(),1), Color.parseColor(Constant.getAppColor().getButBorderColor()));
        //设置背景
        drawable.setColor(Color.parseColor(Constant.getAppColor().getButBackColor()));

        buts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                me.atv.getActivity().gamescene.setVisibility(View.GONE);
                me.atv.getActivity().fullsceneContainer.setVisibility(View.VISIBLE);
                me.atv.getActivity().getFragmentManager().beginTransaction().replace(R.id.fullscene_container,me.atv.getActivity().setColorMainFragment).commitAllowingStateLoss();

            }
        });
        this.atv.getActivity().mBtnGroup.addView(buts);

    }

    public void setUserButs(List<ButsGropObject> buts){
            if(buts==null
                    || buts.size()<=0
                    || this.atv.getActivity().mBtnGroup.getChildCount() <= 0
                    || this.atv.getActivity().getUserBtnAreaAdapter()==null) return;


            List<TitleBean> ts  = new ArrayList<TitleBean>();



            CharSequence charSequenceString;
            //自定义按钮
            for (int i = 0; i < buts.size(); i++) {

                TitleBean t = new TitleBean();


                t.setSpanned(this.atv.getActivity().getHtmlSpanner().fromHtml(buts.get(i).getLabel()));
                //设置标记
                t.setId(i + 1);
                t.setmAction(buts.get(i).getCmd());

                if(this.atv.getActivity().getDiyButs()!=null ){

                    String aryStr[];
                    for(int j=0;j < this.atv.getActivity().getDiyButs().size();j++){
                        if(this.atv.getActivity().getDiyButs().get(j).getKey().equals(String.valueOf(t.getId()))
                                && this.atv.getActivity().getDiyButs().get(j).getParentKey().equals(this.atv.getActivity().getActiveButKey())){
                            //兼容 中文冒号换成英文冒号
                            this.atv.getActivity().getDiyButs().get(j).getCmds().replace("：",":");
                            aryStr = this.atv.getActivity().getDiyButs().get(j).getCmds().split(":");
                            if(aryStr.length==2){

                                charSequenceString = aryStr[0];
                                t.setSpanned(new SpannableString(SpannableString.valueOf(charSequenceString)) );
                                t.setmAction(aryStr[1]);
                            }

                            break;
                        }
                    }
                }

                ts.add(t);
            }




            this.atv.getActivity().getUserBtnAreaAdapter().setData(ts);

            this.atv.getActivity().gdUserBtnArea.setVisibility(View.VISIBLE);
            //隐藏方向键区
            this.atv.getActivity().dirArea.setVisibility(View.GONE);



        }

}
